<template>
   <div class="login">
    <div class="input-format">
      <div class="form_content">
        <!-- 保持原有表单内容不变 -->
        <div class="title_box">
          <h2 class="title">数字管理平台</h2>
        </div>
          <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" clearable type="text" placeholder="wocwin/user">
                <template #prefix>
                  <el-icon>
                    <User />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" placeholder="123456" clearable
                @keyup.enter="handleLogin(loginFormRef)" show-password>
                <template #prefix>
                  <el-icon>
                    <Lock />
                  </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-button :loading="loading" size="default" type="primary" style="width: 100%"
                @click="handleLogin(loginFormRef)">
                <span v-if="!loading">登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>

</template>

<script setup lang="ts" name="login">
// 保持原有脚本内容不变
import SwitchDark from "@/components/switchDark/index.vue";
import Cookies from "js-cookie";
import type { FormInstance } from "element-plus";
import { useUserStore } from "@/stores/module/userInfo";
import { ElNotification } from "element-plus";
import { getTimeState } from "@/utils";
import { getDynamicRouter } from "@/router/dynamicRouter";
import { useTabsStore } from "@/stores/module/tabs";
import { useKeepAliveStore } from "@/stores/module/keepAlive";
import { storeToRefs } from "pinia";
import { useGlobalStore } from "@/stores/module/global";
const globalStore = useGlobalStore();
const { isWatermark } = storeToRefs(globalStore);
const tabStore = useTabsStore();
const keepAliveStore = useKeepAliveStore();
const userStore = useUserStore();

const loginForm = reactive({
  username: "admin",
  password: "123456",
  rememberMe: false,
});
const loginRules = reactive({
  username: [{ required: true, trigger: "blur", message: "用户名不能为空" }],
  password: [{ required: true, trigger: "blur", message: "密码不能为空" }],
});

const username = Cookies.get("username");
const password = Cookies.get("password");
const rememberMe = Cookies.get("rememberMe");
loginForm.username = username ? username : loginForm.username;
loginForm.password = loginForm.password;
loginForm.rememberMe = rememberMe ? Boolean(rememberMe) : false;

const loading = ref(false);
const router = useRouter();
const loginFormRef = ref<FormInstance>();

const handleLogin = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (!valid) return;
    loading.value = true;
    if (loginForm.rememberMe) {
      Cookies.set("username", loginForm.username, { expires: 30 });
      Cookies.set("password", loginForm.password, { expires: 30 });
      Cookies.set("rememberMe", String(loginForm.rememberMe), { expires: 30 });
    } else {
      Cookies.remove("username");
      Cookies.remove("password");
      Cookies.remove("rememberMe");
    }
    userStore
      .Login(loginForm)
      .then(async (res: any) => {
        if (res.success) {
          await getDynamicRouter();
          tabStore.closeMultipleTab();
          keepAliveStore.setKeepAliveName();
          router.push({ path: "/" });
        }
      })
      .finally(() => (loading.value = false));
  });
};
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  height: 100vh;
  background: 
    // linear-gradient(rgba(255,255,255,0.6), rgba(255,255,255,0.6)), // 添加半透明遮罩
    url("@/assets/logo/login2.png") no-repeat center center fixed;
  background-size: cover;
  display: flex;
  justify-content: flex-end; // 内容靠右对齐

  .input-format {
    width: 35%;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.3); // 半透明白色背景
    box-shadow: -2px 0 15px rgba(0,0,0,0.1); // 左侧投影
    
    .form_content {
      width: 80%;
      min-width: 320px;
      padding: 40px;
      border-radius: 12px;
      
      .title {
        font-size: 28px;
        margin-bottom: 40px;
        color: #333;
      }
      
      // 优化表单元素间距
      :deep(.el-form-item) {
        margin-bottom: 28px;
        
        .el-input {
          height: 46px;
          
          input {
            height: 46px;
            font-size: 16px;
          }
        }
      }
      
      .el-button {
        margin-top: 20px;
        height: 46px;
        font-size: 16px;
      }
    }
  }

  // 响应式适配
  @media screen and (max-width: 1200px) {
    .input-format {
      width: 45%;
      
      .form_content {
        width: 90%;
        padding: 30px;
      }
    }
  }

  @media screen and (max-width: 768px) {
    justify-content: center;
    background-position: 60% center;
    
    .input-format {
      width: 90%;
      height: auto;
      min-height: 70vh;
      margin: 20px;
      background: rgba(255,255,255,0.95);
      
      .form_content {
        width: 100%;
        padding: 25px;
      }
    }
  }
}
</style>